#pragma once
// 导入文件题库进入ojserver

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <assert.h>
#include <fstream>

#include "../comm/log.hpp"
#include "../comm/util.hpp"

namespace ns_ojmodel
{
    using namespace ns_log;
    using namespace ns_util;

    struct Question
    {
        std::string number; // 编号
        std::string title;  // 标题
        std::string level;  // 难度
        int cpu_limit;      // 限制cpu最大使用时间
        int as_limit;       // 限制内存最大使用空间
        std::string desc;   // 题目描述
        std::string header; // 给用户预设的代码
        std::string tail;   // 测试用例，用于和用户提交的代码结合提交给服务器编译
    };

    const std::string question_list = "./questions/question.list";
    const std::string question_path = "./questions/";

    class Model
    {
    public:
        Model() {
            //在构造函数中加载列表
            assert(LoadQuestionsList(question_list));
        }
        //加载题目列表
        bool LoadQuestionsList(const std::string& question_list)
        {
            //questions/question_list + 题号
            //打开list文件
            std::ifstream in(question_list);
            if(!in.is_open())
            {
                LOG(FATAL) << "题目文件打开失败,请检查题库是否存在" << "\n";
                return false;
            }   
            //读取
            std::string line;
            while(std::getline(in,line))
            {
                std::vector<std::string> strVec;
                StringUtil::SplitString(line,&strVec," ");
                if(strVec.size() != 5)
                {
                    LOG(WARNING) << "题目读取错误" << "\n";
                    continue;
                }

                //赋值
                Question q;
                //1 判断回文数 简单 1 30000
                q.number = strVec[0];
                q.title = strVec[1];
                q.level = strVec[2];
                q.cpu_limit = atoi(strVec[3].c_str());
                q.as_limit = atoi(strVec[4].c_str());
                
                std::string path = question_path;
                path += q.number;
                path += "/";

                FileUtil::ReadFile(path+"header.cpp",&q.header,true);
                FileUtil::ReadFile(path+"tail.cpp",&q.tail,true);
                FileUtil::ReadFile(path+"desc.txt",&q.desc,true);

                _questions.insert({q.number,q});
            }
            LOG(INFO) << "读取完毕.......OK!" << "\n";
            in.close();
            return true;
        }
        //获取题目列表                  输出型参数
        bool GetAllQuestions(std::vector<Question>* out)
        {
            if(_questions.empty())
            {
                LOG(ERROR) << "获取全部题目列表失败" << "\n";
                return false;
            }
            for(const auto& q:_questions)
            {
                out->push_back(q.second);
            }
            return true;
        }
        //获取一个题目
        bool GetOneQuestion(const std::string& number,Question* out)
        {
            auto find = _questions.find(number);
            if(find == _questions.end())
            {
                //没找到
                LOG(ERROR) << "题目获取失败,题目编号为：" << number << "\n";
                return false;
            }
            (*out) = find->second;
            return true;
        }
        ~Model() {}

    private:
        //题号和题目的映射
        std::unordered_map<std::string,Question> _questions;
    };
}
